﻿using System;
using System.Web;
using GdNet.Core.Assistants;
using GdNet.WebFramework.Business.System;
using GdNet.WebFramework.Domain.System;
using GdNet.WebFramework.Modules.Common.ModuleBase;

namespace GdNet.WebFramework.Modules.Common.Assistants
{
    public sealed class ActionAssistant
    {
        public static ActionDetail Parse(HttpRequestBase request)
        {
            var actionDetail = new ActionDetail
            {
                ModuleCode = request.QueryString[QueryStringParams.Module],
                ActionCode = request.QueryString[QueryStringParams.Action],
                Id = request.QueryString[QueryStringParams.EntityId],
                PageIndex = ConverterAssistant.TryParse(request.QueryString[QueryStringParams.PageIndex]),
                PageSize = ConverterAssistant.TryParse(request.QueryString[QueryStringParams.PageSize]),
            };

            return actionDetail;
        }

        public static IModuleActionService LoadActionService(ModuleAction moduleAction)
        {
            var actionType = Type.GetType(string.Format("{0}, {1}", moduleAction.ClassName, moduleAction.Module.AssemblyName));
            var moduleLoaderService = (IModuleLoaderService)HttpContext.Current.Items["ModuleLoader"];
            return (IModuleActionService)moduleLoaderService.ResolveService(actionType);
        }
    }
}